package fm.dongman.animefm.view.activity

import android.content.Intent
import android.os.Bundle
import com.sina.weibo.sdk.WbSdk
import com.sina.weibo.sdk.auth.AuthInfo
import fm.dongman.animefm.R
import fm.dongman.animefm.view.fragment.*
import fm.dongman.modellib.integrated.QQWBWXLoginHelper
import fm.dongman.modellib.integrated.WBLoginHelper

/**
 * 单独页面 Activity
 * Kotlin 启动语法：
 *      val intent = Intent(activity,SingleActivity::class.java)
 *      intent.putExtra(SingleActivity.EXTRA_NAME, SingleActivity.INTENT_EXTRA_LOGIN)
 *      startActivity(intent)
 *      推荐使用 ActivityHelper.startActivity 启动
 * Created by shize on 2017/10/31.
 */
class SingleActivity : BaseActivity() {

    companion object {
        // key
        val EXTRA_NAME = "fm.dongman.animefm.extra.name" // 启动 activity 的 key
        val BUNDLE_EXTRA_KEY_ID = "fm.dongman.animefm.bundle.extra.id" // 需要传递给 fragment 的 id or 关键字 参数 key
        val BUNDLE_EXTRA_KEY_TYPE = "fm.dongman.animefm.bundle.extra.type" // 需要传递给 fragment 的 type 参数 key
        val BUNDLE_EXTRA_KEY_NAME = "fm.dongman.animefm.bundle.extra.name" // 需要传递给 fragment 的 name 参数 key

        // 特殊
        val INTENT_EXTRA_LOGIN = "fm.dongman.animefm.intent.login" // 登录
        val INTENT_EXTRA_COMIC_CLASSIFY = "fm.dongman.animefm.intent.comicClassify" // 番剧分类页
        val INTENT_EXTRA_QUARTER_COMIC = "fm.dongman.animefm.intent.comicQuarter" // 本季度新番页
        val INTENT_EXTRA_TIME_COMIC = "fm.dongman.animefm.intent.comicTime" // 时间表页
        val INTENT_EXTRA_SEARCH = "fm.dongman.animefm.intent.search" // 搜索页
        val INTENT_EXTRA_SEARCH_RESULT = "fm.dongman.animefm.intent.searchResult" // 搜索结果页
        val INTENT_EXTRA_CHAT = "fm.dongman.animefm.intent.chat" // 私信聊天页
        val INTENT_EXTRA_REGISTER = "fm.dongman.animefm.intent.register" // 注册账号页
        val INTENT_EXTRA_USER_INFO_UPDATE = "fm.dongman.animefm.intent.userupdate"//用户个人信息修改
        val INTENT_EXTRA_CREATE_OR_EDIT_SHEET = "fm.dongman.animefm.intent.createsheet" //创建/修改漫单
        val INTENT_EXTRA_ABOUT_US = "fm.dongman.animefm.intent.aboutus" //关于我们
        // 详情
        val INTENT_EXTRA_USER_INFO = "fm.dongman.animefm.intent.userInfo" // 用户详情页
        val INTENT_EXTRA_SHEET_INFO = "fm.dongman.animefm.intent.sheetInfo" // 漫单详情页
        val INTENT_EXTRA_SPECIAL_INFO = "fm.dongman.animefm.intent.specialInfo" // 专题详情页
        val INTENT_EXTRA_ANIME_INFO = "fm.dongman.animefm.intent.animeInfo" // 动漫详情页
        val INTENT_EXTRA_RANK_INFO = "fm.dongman.animefm.intent.rankInfo" // 漫榜详情页
        // 列表
        val INTENT_EXTRA_ARTICLE_LIST = "fm.dongman.animefm.intent.articleList" // 文章列表页
        val INTENT_EXTRA_COMMENT_LIST = "fm.dongman.animefm.intent.commentList" // 评论列表页
        val INTENT_EXTRA_USER_LIST = "fm.dongman.animefm.intent.userList" // 用户列表页
        val INTENT_EXTRA_CHAT_LIST = "fm.dongman.animefm.intent.chatList" // 聊天列表页
        val INTENT_EXTRA_COMIC_LIST = "fm.dongman.animefm.intent.comicList" // 动漫列表页
        val INTENT_EXTRA_SHEET_LIST = "fm.dongman.animefm.intent.sheetList" // 漫单列表页
        val INTENT_EXTRA_LABEL_SHEET_LIST = "fm.dongman.animefm.intent.labelSheetList" // 特定类型专题列表页
    }

    private var mFragment:BaseFragment? = null // activity中的fragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_single)
        installWbSdk()
        val extra = intent.getStringExtra(EXTRA_NAME)
        when (extra) {
            INTENT_EXTRA_LOGIN -> {
                // 登录
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(LoginFragment.getInstance())
            }
            INTENT_EXTRA_COMIC_CLASSIFY -> {
                // 番剧分类页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(ComicClassifyFragment.getInstance())
            }
            INTENT_EXTRA_QUARTER_COMIC -> {
                // 本季度新番页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(ComicCourierFragment.getInstance())
            }
            INTENT_EXTRA_USER_INFO_UPDATE -> {
                //个人信息修改页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null) {
                    startFragment(UserEditFragment.getInstance())
                }

            }
            INTENT_EXTRA_TIME_COMIC -> {
                // 时间表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(ComicTimeFragment.getInstance())
            }
            INTENT_EXTRA_SEARCH -> {
                // 搜索页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(SearchFragment.newInstance())
            }
            INTENT_EXTRA_SEARCH_RESULT -> {
                // 搜索结果页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(SearchResultFragment.newInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_CHAT -> {
                // 私信聊天页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(ChatFragment.getInstance( intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE),intent.getStringExtra(BUNDLE_EXTRA_KEY_NAME)))
            }
            INTENT_EXTRA_REGISTER -> {
                // 注册账号页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(RegisterFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE)))
            }
            INTENT_EXTRA_USER_INFO -> {
                // 用户详情页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(UserInfoFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_SHEET_INFO -> {
                // 漫单详情页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(SheetDetailsFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_SPECIAL_INFO -> {
                // 专题详情页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(SheetTopicDetailsFragment.newInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_ANIME_INFO -> {
                // 动漫详情页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(AnimeDetailsFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_RANK_INFO -> {
                // 漫榜详情页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(RankInfoFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_ARTICLE_LIST -> {
                // 文章列表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(ArticleListFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE), intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_COMMENT_LIST -> {
                // 评论列表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(CommentListFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE), intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_USER_LIST -> {
                // 用户列表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(UserListFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE), intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_CHAT_LIST -> {
                // 聊天列表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(ChatListFragment.getInstance())
            }
            INTENT_EXTRA_COMIC_LIST -> {
                // 动漫列表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(ComicListFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE), intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_SHEET_LIST -> {
                // 动漫列表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(SheetListFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE), intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_LABEL_SHEET_LIST -> {
                // 特定类型专题列表页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null)
                    startFragment(LabelSheetListFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE), intent.getStringExtra(BUNDLE_EXTRA_KEY_ID)))
            }
            INTENT_EXTRA_CREATE_OR_EDIT_SHEET -> {
                // 创建/编辑漫单页
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null) {
                    startFragment(CreateSheetFragment.getInstance(intent.getStringExtra(BUNDLE_EXTRA_KEY_TYPE)))
                }
            }
            INTENT_EXTRA_ABOUT_US -> {
                // 关于我们
                if (fragmentManager.findFragmentById(R.id.mSingleContent) == null) {
                    startFragment(AboutUsFragment())
                }
            }
        }
    }

    /**
     * 设置新浪登录前置条件
     */
    private fun installWbSdk() {
        if (intent.getStringExtra(EXTRA_NAME) == INTENT_EXTRA_LOGIN)
            WbSdk.install(this, AuthInfo(this, WBLoginHelper.APK_ID, WBLoginHelper.REDIRECT_URL, WBLoginHelper.SCOPE))
    }

    /**
     * 设置 Fragment
     */
    private fun startFragment(fragment: BaseFragment) {
        mFragment = fragment
        val transaction = fragmentManager.beginTransaction()
        transaction.add(R.id.mSingleContent, fragment)
        transaction.commit()
    }

    override fun onResume() {
        super.onResume()
        // 开始加载
        mFragment?.requestListData(1)
    }

    override fun onBackPressed() {
        mFragment?.backResultData()
        super.onBackPressed()
    }

    //当WB—web离开activity时调用
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (intent.getStringExtra(EXTRA_NAME) != INTENT_EXTRA_LOGIN) {
            return
        }
        // qq
        QQWBWXLoginHelper.doQQLoginResult(requestCode, resultCode, data, this)
        // sina
        QQWBWXLoginHelper.doSinaWBLoginResult(requestCode, resultCode, data, this)
    }
}